#include <algorithm>
#include <vector>
#include <string>
#include <string.h>
#include <sstream>
#include "InfoAnalizer.h"
#include "Functions.h"

using namespace std;

void split(const std::string &firstLine, std::vector<std::string> &lines) {
  lines.reserve(7);//7 - number of programs + time quant + 5 times
    
  istringstream iss(firstLine);    
  do     { 
    string sub;     
    iss >> sub; 
    if (sub.empty() == false) {
      lines.push_back(sub);
    }
  } while (iss); 
}  

int InfoAnalizer::getCommandTime(int commandIndex) {
  return values.at(commandIndex);
}

int InfoAnalizer::getNumberOfPrograms() {
  return values.at(0);
}

int InfoAnalizer::getAssigmentTime() {
  return values.at(1);
}

int InfoAnalizer::getPrintTime() {
  return values.at(2);
}

int InfoAnalizer::getLockTime() {
  return values.at(3);
}

int InfoAnalizer::getUnlockTime() {
  return values.at(4);
}

int InfoAnalizer::getEndTime() {
  return values.at(5);
}

int InfoAnalizer::getTimeQuantumSize() {
  return values.at(6);
}  

int getIntValue(const std::string &res) {  
  int intValue = atoi(res.c_str());
  if (intValue == 0 && res != "0") {
    throw std::logic_error("format error");
  }
  return intValue;
}

InfoAnalizer::InfoAnalizer(std::string firstLine) {
  std::vector<std::string> splitVector;
  split(firstLine, splitVector);    
  if (splitVector.size() != 7) {
    throw std::logic_error("wrong first line");
  }
  values.resize(splitVector.size());  

  transform(splitVector.begin(), splitVector.end(), values.begin(), getIntValue);
}

std::vector<int> InfoAnalizer::values;